package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.Discount;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;

public class DiscountDao implements ResultHandler<Discount>{
	private MySqlDb db= MySqlDb.getInstance();
	/**
	 * 根据id修改优惠券数量
	 */
	public boolean updatebyid(int dnum,int did) throws SQLException {
		String sql = "update discount set dnum=? where did=?;";
		return db.executeUpdate(sql,  new GlobalParamHandler(dnum, did)) > 0;
	}
	/**
	 * 根据id修改优惠券数量
	 */
	public boolean xgbyid(String dname,String dtime,String dnum,String ddiscount,String did) throws SQLException {
		String sql = "update discount set dname=?,dtime=?,dnum=?,ddiscount=? where did=?;";
		return db.executeUpdate(sql,  new GlobalParamHandler(dname,dtime,dnum,ddiscount, did)) > 0;
	}
	
	
	/**
	 * 查询所有优惠券信息
	 */
	public List<Discount> findAll() throws SQLException {
		String sql = "select * from  discount";
		return db.executeQuery(sql,this);
	}
	
	/**
	 * 查询前三条优惠券信息
	 */
	public List<Discount> findThree() throws SQLException {
		String sql = "select * from  discount limit 0,3";
		return db.executeQuery(sql,this);
	}
	
	
	
	@Override
	public Discount doHander(Map<String, Object> row) {
		Discount dt=new Discount();
		Integer id=Integer.parseInt(row.get("did")+"") ;
		dt.setdId(id);
		dt.setdName(row.get("dname")+"");
		Integer day=Integer.parseInt(row.get("dtime")+"") ;
		dt.setdTime(day);
		dt.setDescribe(row.get("describe")+"");
		dt.setDpicture(row.get("dpicture")+"");
		Integer num=Integer.parseInt(row.get("dnum")+"") ;
		dt.setDnum(num);
		Double discount=Double.parseDouble(row.get("ddiscount")+"") ;
		dt.setDdiscount(discount);
		return dt;
	}
	
}
